package com.ophiux.common.data.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.ophiux.common.data.tenant.OphiuxTenantConfigProperties;
import com.ophiux.common.data.tenant.OphiuxTenantHandler;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.sql.DataSource;

/**
 * @desc: Mybatis-Plus配置类
 * @author: hhl
 * @date：2023/1/18 16:53
 */
@Configuration
@ConditionalOnBean(DataSource.class)
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
@EnableConfigurationProperties(OphiuxMybatisProperties.class)
public class MybatisPlusConfiguration implements WebMvcConfigurer {

	/**
	 * 增加请求参数解析器，对请求中的参数注入SQL 检查
	 * @param resolverList
	 */
//	@Override
//	public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolverList) {
//		resolverList.add(new SqlFilterArgumentResolver());
//	}

	/**
	 * hncdm 默认数据权限处理器
	 * @return HncdmDefaultDatascopeHandle
	 */
//	@Bean
//	@ConditionalOnMissingBean
//	@ConditionalOnClass(HncdmOauthUser.class)
//	public DataScopeHandle dataScopeHandle() {
//		return new HncdmDefaultDatascopeHandle();
//	}

	/**
	 * mybatis plus 拦截器配置
	 * @return HncdmDefaultDatascopeHandle
	 */
	@Bean
	public MybatisPlusInterceptor mybatisPlusInterceptor(OphiuxTenantConfigProperties tenantConfigProperties) {
		MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
		// 多租户支持
		TenantLineInnerInterceptor tenantLineInnerInterceptor = new TenantLineInnerInterceptor();
		tenantLineInnerInterceptor.setTenantLineHandler(ophiuxTenantHandler(tenantConfigProperties));
		interceptor.addInnerInterceptor(tenantLineInnerInterceptor);
		// 数据权限
//		DataScopeInnerInterceptor dataScopeInnerInterceptor = new DataScopeInnerInterceptor();
//		dataScopeInnerInterceptor.setDataScopeHandle(dataScopeHandle());
//		interceptor.addInnerInterceptor(dataScopeInnerInterceptor);
		// 分页支持
		PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
//		paginationInnerInterceptor.setDbType(DbType.MYSQL);
		paginationInnerInterceptor.setMaxLimit(1000L);
		interceptor.addInnerInterceptor(paginationInnerInterceptor);
		return interceptor;
	}

	/**
	 * 创建租户维护处理器对象
	 * @return 处理后的租户维护处理器
	 */
	@Bean
	@ConditionalOnMissingBean
	public TenantLineInnerInterceptor tenantLineInnerInterceptor(OphiuxTenantConfigProperties tenantConfigProperties) {
		TenantLineInnerInterceptor tenantLineInnerInterceptor = new TenantLineInnerInterceptor();
		tenantLineInnerInterceptor.setTenantLineHandler(new OphiuxTenantHandler(tenantConfigProperties));
		return tenantLineInnerInterceptor;
	}

	/**
	 * 数据权限拦截器
	 * @return DataScopeInterceptor
	 */
//	@Bean
//	@ConditionalOnMissingBean
//	@ConditionalOnClass(OphiuxUser.class)
//	public DataScopeInterceptor dataScopeInterceptor(RemoteDataScopeService dataScopeService) {
//		DataScopeInnerInterceptor dataScopeInnerInterceptor = new DataScopeInnerInterceptor();
//		dataScopeInnerInterceptor.setDataScopeHandle(new PigxDefaultDatascopeHandle(dataScopeService));
//		return dataScopeInnerInterceptor;
//	}


	/**
	 * 创建租户维护处理器对象
	 * @return 处理后的租户维护处理器
	 */
	@Bean
	@ConditionalOnMissingBean(OphiuxTenantConfigProperties.class )
	public OphiuxTenantHandler ophiuxTenantHandler(OphiuxTenantConfigProperties properties) {
		return new OphiuxTenantHandler(properties);
	}

	/**
	 * SQL 日志格式化
	 * @return DruidSqlLogFilter
	 */
	@Bean
	public DruidSqlLogFilter sqlLogFilter(OphiuxMybatisProperties properties) {
		return new DruidSqlLogFilter(properties);
	}

	/**
	 * 字段自动填充
	 * @return {@link MetaObjectHandler}
	 */
	@Bean
	public MybatisPlusMetaObjectHandler mybatisPlusMetaObjectHandler() {
		return new MybatisPlusMetaObjectHandler();
	}


}
